﻿import * as types from './actionTypes'
const SENT = 'sent', RECEIPT = 'receipt';

function updatePage(state, categoryName, increament) {
    var category = state[categoryName];
    var { currengPage } = category;
    currengPage += increament;
    return {
        ...state,
        [categoryName]: { ...category, currengPage }
    }
}

function selectRow(state, { categoryName, row }) {
    var category = state[categoryName];
    var { currentRow } = category;
    if (currentRow === row) return state;

    return {
        ...state,
        [categoryName]: { ...category, currentRow: row }
    }
}

export default function history(state = {}, action) {
    switch (action.type) {
        case types.PRE_SENT_PAGE:
            return updatePage(state, SENT, -1);
        case types.NEXT_SENT_PAGE:
            return updatePage(state, SENT, 1);
        case types.PRE_RECE_PAGE:
            return updatePage(state, RECEIPT, -1);
        case types.NEXT_RECE_PAGE:
            return updatePage(state, RECEIPT, 1);
        case types.SELECT_HIS_ROW:
            return selectRow(state, action);
        default:
            return state;
    }
}